package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.form.*;
import com.tracesystem.controller.info.*;
import com.tracesystem.dao.IssueAttachDao;
import com.tracesystem.model.IssueAttach;
import com.tracesystem.model.IssueBaseInfo;
import com.tracesystem.dao.IssueBaseInfoDao;
import com.tracesystem.service.IssueBaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.CommonsUtils;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.KeywordNumberException;
import com.tracesystem.util.exception.code.IssueInfoExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  缺陷业务接口实现
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-24
 */
@Service
@Transactional
public class IssueBaseInfoServiceImpl extends ServiceImpl<IssueBaseInfoDao, IssueBaseInfo> implements IssueBaseInfoService {
    @Autowired
    IssueAttachDao issueAttachDao;
    /*
     * @Description:新增缺陷
     * @Author: JMOCOK
     * @param issueForm: 缺陷参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result AddIssue(IssueForm issueForm) {
        IssueBaseInfo issueBaseInfo = WoniuCopyUtil.copyOne(issueForm, IssueBaseInfo.class);
        baseMapper.insert(issueBaseInfo);
        if(issueForm.getFileUrl() != null){
            IssueAttach issueAttach = WoniuCopyUtil.copyOne(issueForm, IssueAttach.class);
            issueAttach.setIssueId(issueBaseInfo.getIssueId());
            issueAttachDao.insert(issueAttach);
        }
        return Result.getInstance(IssueBaseInfo.class).setMsg("新增缺陷成功");
    }
    /*
     * @Description:更新缺陷信息
     * @Author: JMOCOK
     * @param modifyIssueForm: 缺陷更新参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result modifyIssueInfo(ModifyIssueForm modifyIssueForm) {
        IssueBaseInfo issueBaseInfo = WoniuCopyUtil.copyOne(modifyIssueForm, IssueBaseInfo.class);
        baseMapper.updateById(issueBaseInfo);
        if(modifyIssueForm.getFileSize()!=null){
            issueAttachDao.delete(new QueryWrapper<IssueAttach>().eq("ISSUE_ID",modifyIssueForm.getIssueId()));
            IssueAttach issueAttach = WoniuCopyUtil.copyOne(modifyIssueForm, IssueAttach.class);

            issueAttachDao.insert(issueAttach);
        }
        return Result.getInstance().setMsg("更新缺陷信息成功");
    }
    /*
     * @Description:删除缺陷记录
     * @Author: JMOCOK
     * @param issueId: 缺陷id
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result removeIssue(Integer issueId) {
        baseMapper.delete(new QueryWrapper<IssueBaseInfo>().eq("ISSUE_ID",issueId));
        return Result.getInstance().setMsg("删除成功");
    }
    /*
     * @Description:查询缺项详情
     * @Author: JMOCOK
     * @param issueId: 缺陷id
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result<IssueDetailInfo> getIssueDetailById(Integer issueId) {
        IssueDetailInfo issueInfo = baseMapper.selectIssueInfoById(issueId);
        return Result.getInstance(IssueDetailInfo.class).setMsg("查询成功").setData(issueInfo);
    }
    /*
     * @Description:查询缺项
     * @Author: JMOCOK
     * @param issueId: 缺陷id
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result<IssueInfo> findIssueById(Integer issueId) {
        IssueBaseInfo issueBaseInfo = baseMapper.selectById(issueId);
        System.out.println(issueBaseInfo);
        IssueInfo issueInfo = WoniuCopyUtil.copyOne(issueBaseInfo, IssueInfo.class);
        return Result.getInstance(IssueInfo.class).setData(issueInfo);
    }

    /*
     * @Description:获取缺陷列表
     * @Author: JMOCOK
     * @param issuePageForm: 缺陷分页查询参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result<IssuePage> findIssuePage(IssuePageForm issuePageForm) {
        IssuePage issuePage = WoniuCopyUtil.copyOne(issuePageForm, IssuePage.class);
        issuePage.setCurrent(issuePageForm.getCurrent());
        issuePage.setSize(issuePageForm.getSize());
        Page<IssueDetailInfo> issueInfoPage = baseMapper.selectIssuePage(issuePage);

        return Result.getInstance(IssuePage.class).setMsg("查询成功").setData(issueInfoPage);
    }
    /*
     * @Description:批量删除
     * @Author: JMOCOK
     * @param batchesDeleteIssueForm: 批量删除参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result removeByIssueIds(BatchesDeleteIssueForm batchesDeleteIssueForm) {
        baseMapper.deleteBatchIds(batchesDeleteIssueForm.getIssueIds());
        return Result.getInstance().setMsg("删除成功");
    }
    /*
     * @Description:分配任务
     * @Author: JMOCOK
     * @param allotTaskForm: 分配任务参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result allotTask(AllotTaskForm allotTaskForm) {
        IssueBaseInfo issueBaseInfo = WoniuCopyUtil.copyOne(allotTaskForm, IssueBaseInfo.class);
        issueBaseInfo.setAssignedTime(CommonsUtils.dateToLocalDateTime(new Date()));
        baseMapper.updateById(issueBaseInfo);
        return Result.getInstance().setMsg("分配任务成功");
    }
    /*
     * @Description:查询缺陷任务列表
     * @Author: JMOCOK
     * @param issuePageform: 分页查询参数
     * @return: com.tracesystem.commons.Result
     **/
    @Override
    public Result<IssuePage> findTaskPage(IssuePageForm issuePageForm) {
        IssuePage issuePage = WoniuCopyUtil.copyOne(issuePageForm, IssuePage.class);
        issuePage.setCurrent(issuePageForm.getCurrent());
        issuePage.setSize(issuePageForm.getSize());
        issuePage.setAssignee(CommonsUtils.getLoginUserInfo().getUsername());
        Page<IssueDetailInfo> issueInfoPage = baseMapper.selectIssuePage(issuePage);
        return Result.getInstance(Result.class).setMsg("查询成功").setData(issueInfoPage);
    }
    /*
     * @Description:查询所有父级缺陷
     * @Author: JMOCOK
     * @return: com.tracesystem.commons.Result<com.tracesystem.controller.info.ParentIssueInfo>
     **/
    @Override
    public Result<ParentIssueInfo> findParentIssueList() {
        List<IssueBaseInfo> parentIssues = baseMapper.selectList(new QueryWrapper<IssueBaseInfo>().eq("PARENT_ISSUE_ID", 0));
        List<ParentIssueInfo> parentIssueInfoList = WoniuCopyUtil.copyMany(parentIssues, ParentIssueInfo.class);
        return Result.getInstanceList(ParentIssueInfo.class).setData(parentIssueInfoList);
    }


    /*
     * @Description:查询缺陷分布情况
     * @Author: JMOCOK
     * @param issueDistributionForm:查询缺陷分布情况参数
     * @return: com.tracesystem.commons.Result<com.tracesystem.controller.info.IssueDistributionInfo>
     **/
    @Override
    public Result<IssueDistributionInfo> getIssueDistribution(IssueDistributionForm issueDistributionForm) {
        if(issueDistributionForm.getKeywordNumber() !=3 && issueDistributionForm.getKeywordNumber() !=2 && issueDistributionForm.getKeywordNumber() !=1){
            throw new KeywordNumberException(IssueInfoExceptionCode.KEYWORD_NUMBER_EXCEPTION);
        }
        List<IssueDistributionInfo> issueDistributionInfos = baseMapper.selectIssueDistribution(issueDistributionForm);
        return Result.getInstanceList(IssueDistributionInfo.class).setData(issueDistributionInfos);
    }

}
